/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmp.ac.lib.tools;

import ac.uitls.propertiesOperation;
import tmp.ac.myInterface.ColorChooser;

//import TMP.ac.acpcland.mian;
import java.awt.Color;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JToolBar;
import javax.swing.border.Border;
import tmp.ac.myInterface.MessageFrame;
import tmp.ac.options.urls;
import tmp.ac.ui.setting.genSetting;
import tmp.ac.ui.setting.menuSetting;
import java.awt.Button;
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.colorchooser.ColorSelectionModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.basic.BasicInternalFrameTitlePane;
import javax.swing.plaf.basic.BasicInternalFrameUI;

/**
 *
 * @author ismail
 */
public class uiUtil extends custemItmes {

    JToolBar jToolBar2;
    JDesktopPane jDesktopPane1;

    public void SetjToolBar(JToolBar jtoolbar) {

        this.jToolBar2 = jtoolbar;


    }

    public void SetjDesktopPane(JDesktopPane jDesktopPane1) {

        this.jDesktopPane1 = jDesktopPane1;


    }
    propertiesOperation op = new propertiesOperation();

    // propertiesOperation op =new propertiesOperation();
    public void openPanel(JPanel parent, JPanel chiled) {

        if (op.getProperty("lang").equals("ar")) {

            chiled.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }

        parent.removeAll();

        chiled.setSize(parent.getSize());
        parent.add(chiled);
        parent.validate();
        parent.repaint();


    }

    public void openMidWindows(final JInternalFrame f, String title) throws IOException {




        JButton button2 = new JButton("X");
        final JToolBar p = new JToolBar();

        // BasicInternalFrameTitlePane titlePane =(BasicInternalFrameTitlePane) ((BasicInternalFrameUI) f.getUI()).getNorthPane();
        // f.remove(titlePane);

        button2.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {



                jToolBar2.remove(p);
                jToolBar2.revalidate();
                jToolBar2.repaint();
                jDesktopPane1.remove(f);
                jDesktopPane1.repaint();

            }
        });


        JLabel l = new JLabel(" " + (op.getWord("label." + title)) + " ");
        //    p.setFloatable(false);
        p.add(l);

        p.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                for (int i = 0; i < jToolBar2.getComponentCount(); i++) {
                    JToolBar mp = (JToolBar) jToolBar2.getComponent(i);
                    mp.setBorder(BorderFactory.createLineBorder(Color.GRAY));
                }
                p.setBorder(BorderFactory.createLineBorder(Color.BLUE));

                jDesktopPane1.setSelectedFrame(f);
                f.toFront();

                jDesktopPane1.repaint();

            }
        });


        p.add(button2);

        p.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        p.setBorder(BorderFactory.createLineBorder(Color.GRAY));

        jToolBar2.add(p);

        jDesktopPane1.add(f);
        f.setMaximizable(true);
        try {
            f.setMaximum(true);
        } catch (PropertyVetoException ex) {
           // Logger.getLogger(mian.class.getName()).log(Level.SEVERE, null, ex);
        }
        if (op.getProperty("lang").equalsIgnoreCase("ar")) {

            f.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);
        }



        f.setVisible(true);
    }

    public void openWindows(JFrame f, int Width, int height) {

        f.setVisible(true);
        f.show();

    }

    public void closeWindows() {
        //    primaryStage.close();
    }

    public void EndPrograme() {
    }

    public static void sendMessage(Component parent, String title, String Message) {

        JOptionPane.showMessageDialog(parent, Message, title, 0);

        /*  MessageFrame f = new MessageFrame(Message);
         f.show();*/
    }

    public static void sendError(Component parent, String title, String Message) {

        JOptionPane.showMessageDialog(parent, Message, title, 0);

        /*  MessageFrame f = new MessageFrame(Message);
         f.show();*/
    }

    public static int sendAlert(Component parent, String title, String Message) {

        return JOptionPane.showConfirmDialog(parent, Message, title, JOptionPane.YES_NO_OPTION);

    }

    // uiUtil u=new util();
    public void updatepanelWords(Component[] coms) {

        //  this.sendMessage("khjk");

        for (Component c : coms) {
            if (c instanceof javax.swing.JButton) {
                javax.swing.JButton d = (javax.swing.JButton) c;

                String[] myname = d.getText().split("_");
                String newna = op.getWord("label." + myname[0]);
                //  this.sendMessage(newna+":"+myname[0]);
                if (newna != null && !newna.trim().equals("")) {
                    d.setText(newna);
                }




            } else if (c instanceof javax.swing.JLabel) {
                javax.swing.JLabel d = (javax.swing.JLabel) c;

                String[] myname = d.getText().split("_");

                String newna = op.getWord("label." + myname[0]);

                if (newna != null && !newna.trim().equals("")) {

                    d.setText(newna);
                }

            } else if (c instanceof javax.swing.JTabbedPane) {
                javax.swing.JTabbedPane d = (javax.swing.JTabbedPane) c;



                for (int i = 0; i < d.getTabCount(); i++) {
                    String[] myname = d.getTitleAt(i).split("_");
                    String newna = op.getWord("label." + myname[0]);
                    d.setTitleAt(i, newna);
                }

                this.updatepanelWords(d.getComponents());

            } else if (c instanceof javax.swing.JPanel) {
                javax.swing.JPanel d = (javax.swing.JPanel) c;
                this.updatepanelWords(d.getComponents());

            } else if (c instanceof javax.swing.JToolBar) {
                javax.swing.JToolBar d = (javax.swing.JToolBar) c;
                this.updatepanelWords(d.getComponents());

            } else if (c instanceof javax.swing.JSplitPane) {
                javax.swing.JSplitPane d = (javax.swing.JSplitPane) c;
                this.updatepanelWords(d.getComponents());

            }



        }
//comps = c.getComponents();
    }

    public void createColorFiled(final JTextField tf) {


        ColorChooser c = new ColorChooser(tf);
        c.show();

    }

    public void clearPanel(Component[] coms) {
        for (Component c : coms) {
            if (c instanceof javax.swing.JTextField) {
                javax.swing.JTextField d = (javax.swing.JTextField) c;

                d.setText("");
            }

            if (c instanceof javax.swing.JTextArea) {
                javax.swing.JTextArea d = (javax.swing.JTextArea) c;

                d.setText("");
            }

        }


    }

    public void clearPanel(Component[] coms, boolean enableStaus) {
        for (Component c : coms) {
            if (c instanceof javax.swing.JTextField) {
                javax.swing.JTextField d = (javax.swing.JTextField) c;

                d.setText("");
                // d.setEnabled(enableStaus);

            }

            if (c instanceof javax.swing.JTextArea) {
                javax.swing.JTextArea d = (javax.swing.JTextArea) c;
                // d.setEnabled(enableStaus);
                d.setText("");
            }

        }


    }

    public void EnablePanel(Component[] coms, boolean enableStaus) {

        for (Component c : coms) {
            if (c instanceof javax.swing.JTextField) {
                javax.swing.JTextField d = (javax.swing.JTextField) c;


                d.setEnabled(enableStaus);

            }

            if (c instanceof javax.swing.JTextArea) {
                javax.swing.JTextArea d = (javax.swing.JTextArea) c;
                d.setEnabled(enableStaus);

            }

        }


    }

    public  static String getFolderPath(String Title) {

        String backUpPath = "";
        JFileChooser fc = null;
        if (fc == null) {
            fc = new JFileChooser();
            fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            fc.setAcceptAllFileFilterUsed(false);
        }
        int returnVal = fc.showDialog(null, Title);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            backUpPath = file.getAbsolutePath();
        }
        return backUpPath;
    }

    public void saveImage(String chDirName, String chFileName) {
        try {

            if (!chDirName.trim().equals("") && !chFileName.trim().equals("")) {
                URI url = this.getClass().getResource(urls.Image_URL).toURI();
                String myurl = url.toString().replace("file:/", "");
                tmp.ac.lib.tools.files.copyfile(chDirName, chFileName, myurl);
            }


        } catch (URISyntaxException ex) {
            Logger.getLogger(menuSetting.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
}
